var swss = {
	ui : {},
	api : {}
};
swss.ui.loading = function(type, text) {
	var $mask = $("#mask");
	var $loding = $("#loading");

	if (type && type == 'hide') {

		$mask.hide();
		$loding.hide();
	} else {

		// $mask.mask();

		$loding.html(text);
		$loding.centerShow();
	}

};

swss.ui.alert = function() {
	$("#msg").dialog({
				title : "提示",
				resizable : false,
				modal : true,
				width : 500,
				height : 400,
				buttons : {
					'保存' : function() {
						$("#formAddUser").submit();
					},
					'取消' : function() {
						$(this).dialog("close");
					}
				}
			});
};
swss.ui.excel = function(txt, callback) {
	$("#msg").html(txt).dialog({
				title : "提示",
				resizable : false,
				modal : true,
				width : 300,
				height : 150,
				text : txt,
				buttons : {
					'确定' : function() {
						if (callback) {
							callback();
							$(this).dialog("close");
						}
					},
					'取消' : function() {
						$(this).dialog("close");
					}
				}
			});
};
swss.ui.confirm = function(txt, callback) {

	$("#msg").html(txt).dialog({
				title : "提示",
				resizable : false,
				modal : true,
				width : 300,
				height : 150,
				text : txt,
				buttons : {
					'确定' : function() {
						if (callback) {
							callback();
						}
					},
					'取消' : function() {
						$(this).dialog("close");
					}
				}
			});
};
swss.ui.del = function(id, name, action, text) {

	var $form = $("#formDel");
	$("#hidId").attr("name", name).val(id);
	$form.attr("action", action);

	if (!text || text == "") {
		text = "确定要删除吗？";

	}

	swss.ui.confirm(text, function() {
				$form.submit();
			});
};

swss.ui.orderBy = function() {

	var type = {
		"desc" : "asc",
		"asc" : "desc"
	};

	var pagination = {
		orderType : "desc",
		orderBy : "id",
		pageno : 1
	};

	var style = {
		"desc" : "ui-icon-circle-arrow-s",
		"asc" : "ui-icon-circle-arrow-n"
	};

	if ($("div.pagebar").size() > 0) {

		var pagebarName = $("div.pagebar").eq(0).attr("name").split("_");

		pagination.orderType = pagebarName[3];
		pagination.orderBy = pagebarName[2];
		pagination.pageno = pagebarName[1];

		var url = $("div.pagebar").eq(0).attr("url");

		// 设置排序字段效果

		$("#th_" + pagination.orderBy).find("span")
				.addClass(style[pagination.orderType]).show();

		/* 跳转到排序链接 */
		$("a[id^='th_']").click(function() {

			pagination.orderBy = $(this).attr("id").split("_")[1];

			location.href = url + "?pagination.pageno=" + pagination.pageno
					+ "&pagination.orderBy=" + pagination.orderBy
					+ "&pagination.orderType=" + type[pagination.orderType];
			return false;
		});
	}
};

swss.ui.button = function() {

};

// 模态框
swss.ui.modal = function(url, options) {
	var o = {
		title : "对话框",
		resizable : false,
		modal : true,
		width : 600,
		height : 550,
		close : function() {
 
		}
	};
	$.extend(o, options);
	var $modal = $("#modal");
	var $iframe=$modal.find("iframe");
	if ($modal.size() == 0) {
		$modal = $("<div id='modal'></div>");
		$("body").append($modal);
		$iframe = $("<iframe frameborder='0' allowtransparency='true'  />");

		$modal.html($iframe);
		// o.height - 40
		
	}
	$iframe.css({
					"height" : "100%",
					"border" : 0
				}).attr(
				"src",
				url + (/\?/.test(url) ? "&" : "?") + "rd="
						+ new Date().getTime());

	$modal.dialog(o);
};
swss.ui.modal.close = function() {
	$("#modal").dialog("close");
};

// toolbar
swss.ui.toolbar = function() {
	var $btnset = $("#btnset");
	if ($btnset.size() != 0) {
		$btnset.buttonset();
	}
};

swss.ui.errorTip = function() {
	setTimeout(function() {
				$("#errorTip").fadeOut("slow");
			}, 2000);
};

/**
 * 表格鼠标上移效果
 */
swss.ui.mouseTr = function() {
	$(".datagird").find("tr").mouseover(function() {
				$(this).addClass("here");
			}).mouseout(function() {
				$(this).removeClass("here");
			});
};

/**
 * 消息提示
 */
swss.ui.msg = function(options) {
	var o = {};

};

swss.ui.tip = function(text) {

	clearTimeout(swss.ui.tip.timer);

	var $tip = $("#tip");
	if ($tip.size() == 0) {
		$tip = $('<div id="tip" class="ui-widget ui-tip"><div class="ui-state-highlight ui-corner-all" ><p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span><span>Sample ui-state-highlight style.</span></p></div></div>');
		$("body").append($tip);
	}
	$tip.find("span").eq(1).html(text);
	$tip.centerShow();

	swss.ui.tip.timer = setTimeout(function() {
				$tip.hide();
			}, 3000);

};
swss.ui.tip.timer = "";

/**
 * 
 */

$.fn.extend({
	/**
	 * 单位树 依赖 jquery treeview
	 * http://localhost:8080/swss/dept/dept_tree.action?deptNo=01
	 */
	deptTree : function(options) {
		var defaults = {
			isLast:true,
			height : 'auto'
		};

		var o = $.extend({}, defaults, options)
		var isOpen = false;
		var $tempInput = "";
		var $treeBox = $('#treeBox');
		if ($treeBox.size() == 0) {
			$treeBox = $('<div class="tree-box" id="treeBox"><a class="btn-close" href="#">关闭</a><ul id="deptTree" class="" ></ul></div>');

			var $deptTree = $treeBox.find("#deptTree");

			if (o.height != 'auto') {
				$deptTree.css({
							height : o.height,
							'overflow' : 'auto'
						});
				$treeBox.find("a.btn-close").css({
							right : 20
						});
			} else {
				$treeBox.find("a.btn-close").css({
							right : 5
						});
			}

			$deptTree.treeview({
						dept : "/swss/dept/dept_tree.action"
					});

			$(document).on('click', "span.dept", function() {
				if (($.trim($(this).attr("rel")) == "*"&&o.isLast)||!o.isLast) {
					$tempInput.val($(this).text());
					$tempInput.next().val($(this).parent().attr("id")
							.split("_")[1]);
				}
			});
			// 关闭方式1
			// $(document).click(function(e) {
			// $treeBox.hide();
			// });
			// $treeBox.find("*").click(function(){
			// return false;
			// });

			// 关闭方式2
			$treeBox.find("a.btn-close").click(function() {
						$treeBox.hide();
						return false;

					});

			$("body").append($treeBox);
		}

		var rePos = function(obj) {
			if (obj) {
				$tempInput = $(obj);
				var offset = $(obj).offset();
				if (($treeBox.outerHeight(true) > ($(window).height()-offset.top + 26))&&(offset.top>$treeBox.outerHeight(true))
						) {
					$treeBox.css({
								top : offset.top - $treeBox.outerHeight(true)
										+ 1,
								left : offset.left
							});
				} else {
					$treeBox.css({
								top : offset.top + 26,
								left : offset.left
							});
				}
			}
		};

		$(window).resize(function() {
					rePos($tempInput);
				});

		this.each(function(i, input) {

					var defaultDeptNo = $(input).attr("data-deptNo");
					if (typeof defaultDeptNo == "undefined") {
						defaultDeptNo = "";
					}
					$(input).after('<input type="hidden" name="'
							+ $(input).attr('name') + '" value="'
							+ defaultDeptNo + '" />');
					$(input).attr('name', $(input).attr("id")).attr("readonly",
							"readonly");

					var thisOpen = false;

					$(input).click(function() {
								if (isOpen) {
									if (thisOpen) {
										$treeBox.hide();
										thisOpen = isOpen = false;
									} else {
										rePos(this);
										thisOpen = isOpen = true;
									}
								} else {
									rePos(this);
									$treeBox.show();
									thisOpen = isOpen = true;
								}

								return false;
							});
				});
	},
	GBTree : function(options) {
		var defaults = {
			height : 'auto',
			nodeEvent : function() {
			}

		};

		var o = $.extend({}, defaults, options)
		var isOpen = false;
		var $tempInput = "";
		var $treeBox = $('#treeBoxGB');
		if ($treeBox.size() == 0) {
			$treeBox = $('<div class="tree-box" id="treeBoxGB"><div class="tree-srh"><input class="txt" type="text" /><input class="btn search" type="button" value="查找" /><input class="btn all" type="button" value="全部" /></div><a class="btn-close" href="#">关闭</a><ul id="GBTree" class="" ></ul><div class="list"></div></div>');

			var $GBTree = $treeBox.find("#GBTree");
			var $list = $treeBox.find("div.list");

			if (o.height != 'auto') {
				$GBTree.css({
							height : o.height,
							'overflow' : 'auto'
						});
				$list.css({
							height : o.height,
							'overflow' : 'auto'
						});
				$treeBox.find("a.btn-close").css({
							right : 20
						});
			} else {
				$treeBox.find("a.btn-close").css({
							right : 5
						});
			}

			$GBTree.treeview({
						url : "/swss/sys/sys_gbtree.action"
					});

			$(document).on('click', "span.node", function() {

				if (!/r/.test($(this).parent().attr("id"))) {
					$tempInput.val($(this).text());
					$tempInput.next().val($(this).parent().attr("id")
							.split("_")[1]);
					o.nodeEvent($(this).attr("value"));
				}
			});

			$(document).on('click', "a.item", function() {

						$tempInput.val($(this).text());
						$tempInput.next().val($(this).attr("id").split("_")[1]);
						o.nodeEvent($(this).attr("value"));
						return false;

					});

			$treeBox.find("a.btn-close").click(function() {
						$treeBox.hide();
						return false;

					});

			// search
			var searchGB = function(keyword) {
				if (keyword == "") {
					return false;
				}
				$.ajax({
							type : "post",
							url : "/swss/sys/sys_gbSearch.action",
							data : {
								gbKeyword : keyword,
								rd : new Date()
							},
							dataType : "json",
							success : function(json) {
								var html = "";
								$(json).each(function(i, gb) {
									html += "<a class='item' id='item_"
											+ gb.ascNo + "' value='" + gb.ascNo
											+ "-" + gb.ascName + "-"
											+ gb.ascSort + "-" + gb.scrapNo
											+ "-" + gb.mareunit
											+ "' href='###'>("
											+ $.trim(gb.ascNo) + ")"
											+ $.trim(gb.ascName) + "</a>";
								});
								$list.html(html);

								$GBTree.hide();
								$list.show();

							}
						});
			};
			$treeBox.find("input.search").click(function() {
						searchGB($.trim($(this).prev().val()));
						return false;
					});
			$treeBox.find("input.all").click(function() {
						$GBTree.show();
						$list.hide();
						return false;
					});

			$("body").append($treeBox);
		}

		var rePos = function(obj) {
			if (obj) {
				$tempInput = $(obj);
				var offset = $(obj).offset();
				if (($treeBox.outerHeight(true) + offset.top + 26) > $("body")
						.height()) {
					$treeBox.css({
								top : offset.top - $treeBox.outerHeight(true)
										+ 1,
								left : offset.left
							});
				} else {
					$treeBox.css({
								top : offset.top + 26,
								left : offset.left
							});
				}
			}
		};

		$(window).resize(function() {
					rePos($tempInput);
				});

		this.each(function(i, input) {

					var defaultGBcode = $(input).attr("data-gbCode");
					if (typeof defaultGBcode == "undefined") {
						defaultGBcode = "";
					}

					$(input).after('<input type="hidden" name="'
							+ $(input).attr('name') + '" value="'
							+ defaultGBcode + '" />');
					$(input).attr('name', $(input).attr("id")).attr("readonly",
							"readonly");

					var thisOpen = false;

					$(input).click(function() {
								if (isOpen) {
									if (thisOpen) {
										$treeBox.hide();
										thisOpen = isOpen = false;
									} else {
										rePos(this);
										thisOpen = isOpen = true;
									}
								} else {
									rePos(this);
									$treeBox.show();
									thisOpen = isOpen = true;
								}

								return false;
							});
				});
	},
	nationList : function(options) {
		var defaults = {
			height : 'auto',
			nodeEvent : function() {
			}

		};

		var o = $.extend({}, defaults, options)
		var isOpen = false;
		var $tempInput = "";
		var $treeBox = $('#listGBM');
		if ($treeBox.size() == 0) {
			$treeBox = $('<div class="tree-box" id="listGBM"><div class="tree-srh"><input class="txt" type="text" /><input class="btn search" type="button" value="查找" /></div><a class="btn-close" href="#">关闭</a><div class="list" style="display:block;" ></div></div>');

			var $list = $treeBox.find("div.list");

			if (o.height != 'auto') {

				$list.css({
							height : o.height,
							'overflow' : 'auto'
						});
				$treeBox.find("a.btn-close").css({
							right : 20
						});
			} else {
				$treeBox.find("a.btn-close").css({
							right : 5
						});
			}

			$(document).on('click', "a.item-gb", function() {

						$tempInput.val($(this).text());
						o.nodeEvent($(this).attr("value"));
						return false;

					});

			$treeBox.find("a.btn-close").click(function() {
						$treeBox.hide();
						return false;

					});

			// search
			var searchGBM = function(keyword) {

				$.ajax({
							type : "post",
							url : "/swss/sys/sys_searchNation.action",
							data : {
								keyword : keyword,
								rd : new Date()
							},
							dataType : "json",
							success : function(json) {
								var html = "";
								$(json).each(function(i, gb) {
									html += "<a class='item-gb' id='item_"
											+ gb.gbm + "' value='" + gb.gbm
											+ "-" + $.trim(gb.gb)
											+ "' href='###'>(" + gb.gbm + ")"
											+ $.trim(gb.gb) + "</a>";
								});
								$list.html(html);

							}
						});
			};
			$treeBox.find("input.search").click(function() {
						searchGBM($.trim($(this).prev().val()));
						return false;
					});

			$("body").append($treeBox);
		}

		var rePos = function(obj) {
			if (obj) {
				$tempInput = $(obj);
				var offset = $(obj).offset();

				if (($treeBox.outerHeight(true) + offset.top + 26) > $("body")
						.height()) {
					$treeBox.css({
								top : offset.top - $treeBox.outerHeight(true)
										+ 1,
								left : offset.left
							});
				} else {
					$treeBox.css({
								top : offset.top + 26,
								left : offset.left
							});
				}
			}
		};

		$(window).resize(function() {
					rePos($tempInput);
				});

		this.each(function(i, input) {

					$(input).attr("readonly", "readonly");

					var thisOpen = false;

					$(input).click(function() {
								if (isOpen) {
									if (thisOpen) {
										$treeBox.hide();
										thisOpen = isOpen = false;
									} else {
										rePos(this);
										thisOpen = isOpen = true;
									}
								} else {
									rePos(this);
									$treeBox.show();
									thisOpen = isOpen = true;
								}

								return false;
							});
				});
		searchGBM("");
	},
	ssList : function(options) {
		var defaults = {
			height : 'auto',
			nodeEvent : function() {
			}

		};

		var o = $.extend({}, defaults, options)

		var $tempInput = "";
		var $treeBox = $('#listSS');
		var isOpen = false;

		if ($treeBox.size() == 0) {
			$treeBox = $('<div class="tree-box" id="listSS"><div class="tree-srh"><input class="txt" type="text" /><input class="btn search" type="button" value="查找" /></div><a class="btn-close" href="#">关闭</a><div class="list" style="display:block;" ></div></div>');

			var $list = $treeBox.find("div.list");

			if (o.height != 'auto') {

				$list.css({
							height : o.height,
							'overflow' : 'auto'
						});
				$treeBox.find("a.btn-close").css({
							right : 20
						});
			} else {
				$treeBox.find("a.btn-close").css({
							right : 5
						});
			}

			$(document).on('click', "a.item-ss", function() {

						$tempInput.val($(this).text());
						o.nodeEvent($(this).attr("value"));
						return false;

					});

			$treeBox.find("a.btn-close").click(function() {
						$treeBox.hide();
						isOpen = false;

						return false;

					});

			// search
			var search = function(keyword) {

				$.ajax({
							type : "post",
							url : "/swss/sys/sys_searchStorage.action",
							data : {
								keyword : keyword,
								deptNo : $("#deptName").next().val(),
								rd : new Date()
							},
							dataType : "json",
							success : function(json) {
								var html = "";
								$(json).each(function(i, ss) {
									html += "<a class='item-ss' id='item_"
											+ ss.ssNo + "' value='" + ss.ssNo
											+ "-" + $.trim(ss.ssName)
											+ "' href='###'>(" + ss.ssNo + ")"
											+ $.trim(ss.ssName) + "</a>";
								});
								$list.html(html);

							}
						});
			};
			$treeBox.find("input.search").click(function() {
						search($.trim($(this).prev().val()));
						return false;
					});

			$("body").append($treeBox);
		}

		var rePos = function(obj) {
			if (obj) {
				$tempInput = $(obj);
				var offset = $(obj).offset();
				if (($treeBox.outerHeight(true) + offset.top + 26) > $("body")
						.height()) {
					$treeBox.css({
								top : offset.top - $treeBox.outerHeight(true)
										+ 1,
								left : offset.left
							});
				} else {
					$treeBox.css({
								top : offset.top + 26,
								left : offset.left
							});
				}
			}
		};

		$(window).resize(function() {
					rePos($tempInput);
				});

		this.each(function(i, input) {

					$(input).attr("readonly", "readonly");

					var thisOpen = false;

					$(input).click(function() {
								if (isOpen) {
									if (thisOpen) {
										$treeBox.hide();
										thisOpen = isOpen = false;
									} else {
										rePos(this);
										thisOpen = isOpen = true;
									}
								} else {
									rePos(this);
									$treeBox.show();
									thisOpen = isOpen = true;
								}

								return false;
							});
				});
		search("");
	}
});

/**
 * datagird工具
 */
var DG = {
	// 设置全选 复选框
	setCheckBox : function() {
		var $chkItems = $("input[name='chkItem']");
		$("#chkAll").click(function() {
					if ($(this).attr("checked")) {
						$chkItems.attr("checked", true);
					} else {
						$chkItems.attr("checked", false);
					}
				});
	},
	// 是否有选中的复选框
	isChecked : function() {
		var $checkedItems = $("input[name='chkItem']:checked");
		if ($checkedItems.size() == 0) {
			return false;
		}
		return true;
	},
	// 获取选中复选框的value
	getItemsValue : function() {
		var $checkedItems = $("input[name='chkItem']:checked");
		var arr = [];
		$checkedItems.each(function() {
					arr.push($(this).val());
				});
		return arr;
	},
	getItemsNo : function() {
		var $checkedItems = $("input[name='chkItem']:checked");
		var arr = [];
		$checkedItems.each(function() {
					arr.push($(this).attr("data-no"));
				});
		return arr;
	}
};

var searchBar = {
	addEvent : function() {
		$("#btnSearch").click(function() {
			swss.ui.modal(
					"/swss/asset/search_searchController.action?searchType="
							+ $(this).attr("data"), {
						title : "通用查询",
						width : 736,
						height : 480
					});
			return false;
		});

		$("#btnSearchComplex").click(function() {
			swss.ui.modal(
					"/swss/asset/search_searchComplexController.action?searchType="
							+ $(this).attr("data"), {
						title : "高级查询",
						width : 700,
						height : 350
					});

			return false;
		});
	},
	init : function() {
		this.addEvent();
	}
};

$(function() {

			// 列表页工具条
			swss.ui.toolbar();

			// 列表页排序功能
			swss.ui.orderBy();

			// 成功/错误提示 效果
			swss.ui.errorTip();

			// 表格鼠标上移
			swss.ui.mouseTr();

			if (typeof res != "undefined") {
				if (res.code == 0||res.code=='0') {
					swss.ui.tip(res.msg);
				}
			}
		});
